import { defineStore } from 'pinia'
import seekerApi from '@/api/seeker'
import { useUserStore } from './user'
import { useAuthStore } from './auth'

export const useSeekerStore = defineStore('seeker', {
  state: () => ({
    seeker: null,
    job_intentions: [],
    education_experiences: [],
    project_experiences: [],
    work_experiences: [],
  }),
  getters: {},
  actions: {
    async getSeeker() {
      try {
        const userStore = useUserStore()
        const res = await seekerApi.getSeeker(userStore.user_id)
        this.seeker = res.data
        useAuthStore().setHasName(this.seeker.name!==null)
        return Promise.resolve(res.data)
      } catch (error) {
        return Promise.reject(error)
      }
    },
    async getEducationExperience() {
      try {
        const userStore = useUserStore()
        const res = await seekerApi.getEducationExperience(userStore.user_id)
        this.education_experiences = res.data
        return Promise.resolve(res.data)
      } catch (error) {
        return Promise.reject(error)
      }
    },
    async getProjectExperience() {
      try {
        const userStore = useUserStore()
        const res = await seekerApi.getProjectExperience(userStore.user_id)
        this.project_experiences = res.data
        return Promise.resolve(res.data)
      } catch (error) {
        return Promise.reject(error)
      }
    },
    async getWorkExperience() {
      try {
        const userStore = useUserStore()
        const res = await seekerApi.getWorkExperience(userStore.user_id)
        this.work_experiences = res.data
        return Promise.resolve(res.data)
      } catch (error) {
        return Promise.reject(error)
      }
    },
    async getJobIntention() {
      try {
        const userStore = useUserStore()
        const res = await seekerApi.getJobIntention(userStore.user_id)
        this.job_intentions = res.data
        return Promise.resolve(res.data)
      } catch (error) {
        return Promise.reject(error)
      }
    },
    async getSeekerResume() {
      try {
        const userStore = useUserStore()
        const res = await seekerApi.getResume(userStore.user_id)
        const {
          seeker,
          job_intentions,
          education_experiences,
          project_experiences,
          work_experiences,
        } = res.data || {}
        this.seeker = seeker
        this.job_intentions = job_intentions
        this.education_experiences = education_experiences
        this.project_experiences = project_experiences
        this.work_experiences = work_experiences
        return Promise.resolve(res.data)
      } catch (error) {
        return Promise.reject(error)
      }
    },
    //更新信息
    updateSeeker(data) {
      this.seeker = {... data}
    },
    resetSeeker() {
      this.$reset()
    },
  },
})
